class Solution
{
private:
    unordered_map<char, int> pr = {
        {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};

public:
    void count(stack<char> &st, stack<int> &st_num)
    {
        int b = st_num.top();
        st_num.pop();
        int a = st_num.top();
        st_num.pop();
        char op = st.top();
        st.pop();
        if (op == '+')
            st_num.push(a + b);
        else if (op == '-')
            st_num.push(a - b);
        else if (op == '*')
            st_num.push(a * b);
        else
            st_num.push(a / b);
    }
    void eval(stack<char> &st, stack<int> &st_num, char now_op)
    {
        while (st.size() && pr[st.top()] >= pr[now_op])
            count(st, st_num);
        st.push(now_op);
    }
    int calculate(string s)
    {
        stack<int> st_num;
        stack<char> st;
        for (int i = 0; i < s.size(); ++i)
        {
            // cout << i << endl;
            if (isspace(s[i]))
                continue;
            if (isdigit(s[i]))
            {
                int num = 0;
                while (isdigit(s[i]))
                {
                    num = num * 10 + (s[i] - '0');
                    ++i;
                }
                --i;
                st_num.push(num);
            }
            else
            {
                if (st.empty() || pr[s[i]] > pr[st.top()])
                    st.push(s[i]);
                else
                    eval(st, st_num, s[i]);
            }
        }
        // cout << "---" << endl;
        while (st.size())
            count(st, st_num);
        // cout << st_num.top() << endl;
        return st_num.top();
    }
};